package com.xbongbong.pay.platform.alipay.utils;

import com.alipay.api.AlipayApiException;
import com.alipay.api.domain.AlipayTradeCancelModel;
import com.alipay.api.domain.AlipayTradePagePayModel;
import com.alipay.api.domain.AlipayTradePageRefundModel;
import com.alipay.api.domain.AlipayTradePrecreateModel;
import com.alipay.api.domain.AlipayTradeRefundModel;
import com.alipay.api.domain.AlipayTradeWapPayModel;
import com.alipay.api.request.AlipayTradeCancelRequest;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradePageRefundRequest;
import com.alipay.api.request.AlipayTradePrecreateRequest;
import com.alipay.api.request.AlipayTradeRefundRequest;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.alipay.api.response.AlipayTradeCancelResponse;
import com.alipay.api.response.AlipayTradePagePayResponse;
import com.alipay.api.response.AlipayTradePageRefundResponse;
import com.alipay.api.response.AlipayTradePrecreateResponse;
import com.alipay.api.response.AlipayTradeRefundResponse;
import com.alipay.api.response.AlipayTradeWapPayResponse;
import com.xbongbong.pay.platform.alipay.config.AliPayMeta;
import com.xbongbong.pay.platform.alipay.config.AliPayMetaContainer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Objects;

/**
 *
 * @author 魏荣杰
 * @date 2021/1/15 16:21
 * @since
 * @version
 */
@Component
public class AliPayApi {
    private static final Logger LOG = LoggerFactory.getLogger(AliPayApi.class);

    @Resource
    private AliPayMetaContainer aliPayMetaContainer;

    /**
     * Description: 统一收单线下交易预创建
     * @param model
     * @param returnUrl
     * @param notifyUrl
     * @param mchId
     * @return com.alipay.api.response.AlipayTradePrecreateResponse
     * @throws
     * @author 魏荣杰
     * @date 2021/2/4 15:45
     * @since
     */
    public AlipayTradePrecreateResponse tradePrecreate(AlipayTradePrecreateModel model, String returnUrl, String notifyUrl, String mchId) throws AlipayApiException {
        AliPayMeta aliPayMeta = aliPayMetaContainer.getAliPayMeta(mchId);
        AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest();
        request.setNotifyUrl(notifyUrl);
        request.setReturnUrl(returnUrl);
        request.setBizModel(model);
        return AliPayClient.doExecute(request, aliPayMeta);
    }

    /**
     * WAP支付
     *
     * @param model        {@link AlipayTradeWapPayModel}
     * @param returnUrl    异步通知URL
     * @param notifyUrl    同步通知URL
     * @param appAuthToken 应用授权token
     * @param mchId 商户id
     * @return {String}
     * @throws AlipayApiException 支付宝 Api 异常
     */
    public AlipayTradeWapPayResponse wapPay(AlipayTradeWapPayModel model, String returnUrl, String notifyUrl, String appAuthToken, String mchId) throws AlipayApiException {
        AliPayMeta aliPayMeta = aliPayMetaContainer.getAliPayMeta(mchId);
        AlipayTradeWapPayRequest aliPayRequest = new AlipayTradeWapPayRequest();
        aliPayRequest.setReturnUrl(returnUrl);
        aliPayRequest.setNotifyUrl(notifyUrl);
        aliPayRequest.setBizModel(model);
        if (Objects.nonNull(appAuthToken)) {
            aliPayRequest.putOtherTextParam("app_auth_token", appAuthToken);
        }
        return AliPayClient.pageExecute(aliPayRequest, aliPayMeta);
    }

    /**
     * 统一收单下单并支付页面接口 <br>
     *
     * @param model        {AlipayTradePayModel}
     * @param notifyUrl    异步通知URL
     * @param appAuthToken 应用授权token
     * @param mchId 商户id
     * @return {AlipayTradePayResponse}
     * @throws AlipayApiException 支付宝 Api 异常
     */
    public AlipayTradePagePayResponse tradePagePay(AlipayTradePagePayModel model, String returnUrl, String notifyUrl, String appAuthToken, String mchId) throws AlipayApiException {
        AliPayMeta aliPayMeta = aliPayMetaContainer.getAliPayMeta(mchId);
        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
        request.setBizModel(model);
        request.setReturnUrl(returnUrl);
        request.setNotifyUrl(notifyUrl);
        if (Objects.nonNull(appAuthToken)) {
            request.putOtherTextParam("app_auth_token", appAuthToken);
        }
        return AliPayClient.pageExecute(request, aliPayMeta);
    }

    /**
     * 统一收单交易退款接口
     *
     * @param model        {@link AlipayTradeRefundModel}
     * @param appAuthToken 应用授权token
     * @param mchId 商户id
     * @return {@link AlipayTradeRefundResponse}
     * @throws AlipayApiException 支付宝 Api 异常
     */
    public AlipayTradeRefundResponse tradeRefund(AlipayTradeRefundModel model, String appAuthToken, String mchId) throws AlipayApiException {
        AliPayMeta aliPayMeta = aliPayMetaContainer.getAliPayMeta(mchId);
        AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
        request.setBizModel(model);
        return AliPayClient.doExecute(request, appAuthToken, aliPayMeta);
    }


    /**
     * 统一收单退款页面接口
     *
     * @param model        {@link AlipayTradePageRefundModel}
     * @param appAuthToken 应用授权token
     * @param mchId 商户id
     * @return {@link AlipayTradePageRefundResponse}
     * @throws AlipayApiException 支付宝 Api 异常
     */
    public AlipayTradePageRefundResponse tradePageRefund(AlipayTradePageRefundModel model, String appAuthToken, String mchId) throws AlipayApiException {
        AliPayMeta aliPayMeta = aliPayMetaContainer.getAliPayMeta(mchId);

        AlipayTradePageRefundRequest request = new AlipayTradePageRefundRequest();
        request.setBizModel(model);
        return AliPayClient.doExecute(request, appAuthToken, aliPayMeta);
    }

    /**
     * 统一收单交易撤销接口
     *
     * @param model        {@link AlipayTradeCancelModel}
     * @param appAuthToken 应用授权token
     * @param mchId 商户id
     * @return {@link AlipayTradeCancelResponse}
     * @throws AlipayApiException 支付宝 Api 异常
     */
    public AlipayTradeCancelResponse tradeCancel(AlipayTradeCancelModel model, String appAuthToken, String mchId) throws AlipayApiException {
        AliPayMeta aliPayMeta = aliPayMetaContainer.getAliPayMeta(mchId);

        AlipayTradeCancelRequest request = new AlipayTradeCancelRequest();
        request.setBizModel(model);
        return AliPayClient.doExecute(request, appAuthToken, aliPayMeta);
    }
}
